package com.dap.cms.governance.service.impl;

import com.dap.cms.base.impl.BaseServiceImpl;
import com.dap.cms.governance.common.RequestHolder;
import com.dap.cms.governance.po.CmsMessagePo;
import com.dap.cms.governance.service.CmsMessageService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @description: 类描述
 * @author: tiger
 * @createTime:2018/6/12
 * @update:修改内容
 * @author: tiger
 * @updateTime:2018/6/12
 */
@Service
public class CmsMessageServiceImpl extends BaseServiceImpl<CmsMessagePo> implements
        CmsMessageService {

    @Override
    public void addMsg(Integer toUserId, Integer msgType, String title, String msg, String
            otherMsg) {
        CmsMessagePo po = new CmsMessagePo();
        po.setFromUserId(RequestHolder.getCurrentUser().getId());
        po.setToUserId(toUserId);
        po.setMsgType(msgType);
        po.setTitle(title);
        po.setMsg(msg);
        po.setOtherMsg(otherMsg);
        po.setSendTime(new Date());
        po.setReadFlag(0);
        super.save(po);
    }

    @Override
    public void changeFlag(String ids) {
        String[] strArray = ids.split(",");
        for (String id : strArray
                ) {
            CmsMessagePo po = new CmsMessagePo();
            po.setId(Integer.parseInt(id));
            po = get(po);
            po.setReadFlag(1);
            update(po);
        }
    }

    @Override
    public void deleteMsg(String ids) {
        String[] strArray = ids.split(",");
        for (String id : strArray
                ) {
            CmsMessagePo po = new CmsMessagePo();
            po.setId(Integer.parseInt(id));
            delete(po);
        }
    }

    @Override
    public void batchSendMsg(Integer projectId, Integer groupId, Integer type, String
            consumerGroup, String title, String msg, String otherMsg) {
      /*  if (projectId == null || groupId == null) {
            return;
        }
        //根据契约获取所有项目成员
        ServiceProjectUserPo projectUserPo = new ServiceProjectUserPo();
        projectUserPo.setGroupId(groupId);
        Map<String, Object> paramMap = new HashMap<>(16);
        paramMap.put("projectId", projectId);
        String[] groupCodes = consumerGroup.split(",");
        paramMap.put("groupCodes", groupCodes);
        List<ServiceProjectGroupPo> groupPoList = daoService.selectList(ServiceProjectGroupPo
                .class.getName() + ".selectListInGroupCodes", paramMap);
        List<String> groupIds = groupPoList.stream().map(temp -> temp.getId() + "")
                .collect(Collectors.toList());
        paramMap.put("groupIds", groupIds);
        paramMap.put("userId", RequestHolder.getCurrentUser().getId());
        List<ServiceProjectUserPo> serviceProjectUserPos = daoService.selectList
                (ServiceProjectUserPo
                        .class.getName() + ".selectListInGroups", paramMap);
        for (ServiceProjectUserPo serviceProjectUserPo : serviceProjectUserPos
                ) {
            this.addMsg(
                    serviceProjectUserPo.getUserId(),
                    type,
                    title,
                    msg,
                    otherMsg
            );
        }*/
    }
}
